Flutter / Examples / Login & Register 3 : Reusable Widgets
Login & Register 1
-
Steps
1. login dart
import 'package:flutter/material.dart'; class MyLogin extends StatefulWidget { const MyLogin({Key? key}) : super(key: key); @override _MyLoginState createState() => _MyLoginState(); } class _MyLoginState extends State { @override Widget build(BuildContext context) { return Container( decoration: const BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/login.png'), fit: BoxFit.cover), ), child: Scaffold( backgroundColor: Colors.transparent, body: Stack(children: [ Container( padding: const EdgeInsets.only(left: 35, top: 80), child: const Text( "Welcome\nBack", style: TextStyle(color: Colors.white, fontSize: 33), ), ), SingleChildScrollView( child: Container( padding: EdgeInsets.only( right: 35, left: 35, top: MediaQuery.of(context).size.height * 0.5), child: Column(children: [ TextField( decoration: InputDecoration( fillColor: Colors.grey.shade100, filled: true, hintText: 'Email', border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), ), ), ), const SizedBox( height: 30, ), TextField( obscureText: true, decoration: InputDecoration( fillColor: Colors.grey.shade100, filled: true, hintText: 'Password', border: OutlineInputBorder( borderRadius: BorderRadius.circular(10), ), ), ), const SizedBox( height: 40, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( 'Sign In', style: TextStyle( color: Color(0xff4c505b), fontSize: 27, fontWeight: FontWeight.w700, ), ), CircleAvatar( radius: 30, backgroundColor: const Color(0xff4c505b), child: IconButton( color: Colors.white, onPressed: () {}, icon: const Icon(Icons.arrow_forward), ), ), ], ), const SizedBox( height: 40, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ TextButton( onPressed: () { Navigator.pushNamed(context, 'register'); }, child: const Text( 'Sign Up', style: TextStyle( decoration: TextDecoration.underline, fontSize: 18, color: Color(0xff4c505b), ), ), ), TextButton( onPressed: () {}, child: const Text( 'Forgot Password', style: TextStyle( decoration: TextDecoration.underline, fontSize: 18, color: Color(0xff4c505b), ), ), ), ]), ]), ), ), ]), ), ); } } 1. set background image to the page
decoration: const BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/login.png'), fit: BoxFit.cover), ) 2. register dart
import 'package:flutter/material.dart'; class MyRegister extends StatefulWidget { const MyRegister({Key? key}) : super(key: key); @override _MyRegisterState createState() => _MyRegisterState(); } class _MyRegisterState extends State { @override Widget build(BuildContext context) { return Container( decoration: const BoxDecoration( image: DecorationImage( image: AssetImage('assets/images/register.png'), fit: BoxFit.cover), ), child: Scaffold( appBar: AppBar( backgroundColor: Colors.transparent, elevation: 0, ), backgroundColor: Colors.transparent, body: Stack(children: [ Container( padding: const EdgeInsets.only(left: 35, top: 80), child: const Text( "Create\nAccount", style: TextStyle(color: Colors.white, fontSize: 33), ), ), SingleChildScrollView( child: Container( padding: EdgeInsets.only( right: 35, left: 35, top: MediaQuery.of(context).size.height * 0.27), child: Column(children: [ TextField( decoration: InputDecoration( focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide(color: Colors.black), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide(color: Colors.white), ), hintText: 'Name', hintStyle: const TextStyle(color: Colors.white), ), ), const SizedBox( height: 30, ), TextField( decoration: InputDecoration( focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide(color: Colors.black), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide(color: Colors.white), ), hintText: 'Email', hintStyle: const TextStyle(color: Colors.white), ), ), const SizedBox( height: 30, ), TextField( obscureText: true, decoration: InputDecoration( focusedBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide(color: Colors.black), ), enabledBorder: OutlineInputBorder( borderRadius: BorderRadius.circular(10), borderSide: const BorderSide(color: Colors.white), ), hintText: 'Password', hintStyle: const TextStyle(color: Colors.white), ), ), const SizedBox( height: 40, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ const Text( 'Sign In', style: TextStyle( color: Colors.white, fontSize: 27, fontWeight: FontWeight.w700, ), ), CircleAvatar( radius: 30, backgroundColor: const Color(0xff4c505b), child: IconButton( color: Colors.white, onPressed: () {}, icon: const Icon(Icons.arrow_forward), ), ), ]), const SizedBox( height: 40, ), Row( mainAxisAlignment: MainAxisAlignment.spaceBetween, children: [ TextButton( onPressed: () { Navigator.pushNamed(context, 'login'); }, child: const Text( 'Login', style: TextStyle( decoration: TextDecoration.underline, fontSize: 18, color: Colors.white, ), ), ), ]), ]), ), ), ]), ), ); } } 3. main dart
import 'package:flutter/material.dart'; import 'package:flutterapp/login.dart'; import 'package:flutterapp/register.dart'; void main() { runApp( MaterialApp( debugShowCheckedModeBanner: false, initialRoute: 'login', routes: { 'login': (context) => const MyLogin(), 'register': (context) => const MyRegister(), }), ); }
MANVIA BLOG